<!DOCTYPE html>
<html lang="en">
    <head>
        <meta charset="UTF-8" />
        <meta http-equiv="X-UA-Compatible" content="IE=edge" />
        <meta name="viewport" content="width=device-width, initial-scale=1.0" />
        <title>展开运算符</title>
    </head>
    <body>
        <script type="text/javascript">
            let arr1 = [1, 3, 5, 7, 9];
            let arr2 = [2, 4, 6, 8, 10];

            // 展开一个数组
            console.log(...arr1);

            // 连接数组
            let arr3 = [...arr1, ...arr2];
            console.log(arr3);

            // 函数传参
            function sum(...numbers) {
                console.log(numbers);
                return numbers.reduce((preValue, currentValue) => {
                    return preValue + currentValue;
                });
            }

            console.log(sum(1, 2, 3, 4));

            // 无法直接展开对象
            let person = { name: "Tom", age: 18 };
            // 使用字面量方式复制对象
            let person2 = { ...person };
            // 报错，展开运算符不能展开对象
            // console.log(...person);
            person.name = "Jerry";
            console.log(person2);
            console.log(person);

            // 合并其它属性
            let person3 = { ...person, name: "Jack", address: "地球" };
            console.log(person3);
        </script>
    </body>
</html>
